<template>
  <el-container class="home">
    <el-header>
      <div class="boxx">
        <img src="../assets/heima.png" alt>
        <span class="small">电商管理后台系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>

    </el-header>
    <el-container>
      <el-aside :width="toggleCollapse?'64px':'200px'">
       <div @click="fn" class="toggle" >|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened="true" :collapse="toggleCollapse" :collapse-transition="false" :default-active="activePath" router>
          
          <el-submenu :index="item.id+''" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i class="icon" :class="iconClass[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由站位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>

export default {
  data() {
    return {
        menusList:[],
        iconClass:{
            '125': 'iconfont icon-users',
            '103': 'iconfont icon-tijikongjian',
            '101': 'iconfont icon-shangpin',
            '102': 'iconfont icon-danju',
            '145': 'iconfont icon-baobiao',

        },
         toggleCollapse: false,
         activePath:''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
        logout() {
          sessionStorage.removeItem('token')
          this.$router.push('/login')
        },
        async getMenuList(){
            const {data:res} = await this.axios.get('menus')
            
            if(res.meta.status !=200){
                return this.$message.error('服务器异常')
                this.menulist = res.data
                console.log(res)
            }
            this.menusList = res.data
            this.$message.success('亲.获取数据成功了')
        },
        // setDefActive(path) {
        //   sessionStorage.setItem('path',path)
        //   this.defActive = path
        fn() {
          this.toggleCollapse = !this.toggleCollapse
        },
        saveNavState(activePath) {
          window.sessionStorage.setItem('activePath',activePath)
          this.activePath= activePath
        }
    }
  }

  // created() {
  //     this.defActive= sessionStorage.getItem('path')
  //     this.getMenuList();
  // }
</script>

<style lang='less' scoped>
.home {
  height: 100%;
}
.el-header {
  background: #333744;
  display: flex;
  justify-content: space-between;
}
.el-aside {
  background: #333744;
}
.el-main {
  background: yellow;
}
.small {
  font-size: 20px;
  color: #fff;
  margin-left: 20px;
}
.boxx {
  display: flex;
  align-items: center;
}
.el-menu{
    border-right: none;
}
.toggle{
    background: #333744;
    padding: 5px 0;
    text-align: center;
    cursor: pointer;
    letter-spacing: 6px;
    color: #fff;

}
</style>
